// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <Work.hpp>

#include <cstring>

const uint8_t Work::ID_ACTOR    = 0x01;
const uint8_t Work::ID_DIRECTOR = 0x02;
const uint8_t Work::ID_PRODUCER = 0x04;
const uint8_t Work::ID_WRITER   = 0x08;
const uint8_t Work::ID_UNKNOWN  = 0x80;
const uint8_t Work::ID_ALL      = 0xFF;

const char * Work::TAG_ACTOR    = "Actor";
const char * Work::TAG_DIRECTOR = "Director";
const char * Work::TAG_PRODUCER = "Producer";
const char * Work::TAG_WRITER   = "Writer";
const char * Work::TAG_UNKNOWN  = "Unknown";
const char * Work::TAG_ALL      = "All";

Work::Work() : _work(ID_UNKNOWN) 
{
}

Work::Work(const uint8_t work) : _work(work)
{
}

Work::Work(const char * work) 
{
  if ( !strcasecmp(work, TAG_ACTOR) )
    _work = ID_ACTOR;
  else if ( !strcasecmp(work, TAG_DIRECTOR) )
    _work = ID_DIRECTOR;
  else if ( !strcasecmp(work, TAG_PRODUCER) )
    _work = ID_PRODUCER;
  else if ( !strcasecmp(work, TAG_WRITER) )
    _work = ID_WRITER;
  else if ( !strcasecmp(work, TAG_ALL) )
    _work = ID_ALL;
  else
    _work = ID_UNKNOWN;
}

Work& Work::operator = (const uint8_t work)
{
  _work = work;
  return *this;
}

bool Work::operator == (const Work& work) const
{
  return ( work._work == _work );
}

bool Work::operator == (const uint8_t work) const
{
  return ( work == _work );
}

bool Work::operator != (const Work& work) const
{
  return ( work._work != _work );
}

bool Work::operator != (const uint8_t work) const
{
  return ( work != _work );
}

Work::operator uint8_t () const
{
  return _work;
}

Work::operator const char * () const
{
  switch(_work) {
  case ID_ACTOR:
    return TAG_ACTOR;
  case ID_DIRECTOR:
    return TAG_DIRECTOR;
  case ID_PRODUCER:
    return TAG_PRODUCER;
  case ID_WRITER:
    return TAG_WRITER;
  case ID_ALL:
    return TAG_ALL;
  default:
    return TAG_UNKNOWN;
  }
}

bool Work::mask(const Work& work_a, const Work& work_b)
{
  /* Bit-wise AND */
  return ( (work_a._work & work_b._work) != 0x00 );
}
